<%#
 Copyright 2013-2025 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      https://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
<div>
    <h2 id="page-heading" data-cy="<%- entityNameCapitalized %>Heading">
        <span>__jhiTranslateTag__('<%- i18nKeyPrefix %>.home.title')</span>

        <div class="d-flex justify-content-end">
            <button class="btn btn-info me-2" (click)="load()" [disabled]="isLoading">
                <fa-icon icon="sync" [animation]="isLoading ? 'spin' : undefined" />
                <span>__jhiTranslateTag__('<%- i18nKeyPrefix %>.home.refreshListLabel')</span>
            </button>
            <%_ if (!readOnly) { _%>

            <button id="jh-create-entity" data-cy="entityCreateButton" class="btn btn-primary jh-create-entity create-<%= entityUrl %>" [routerLink]="['/<%= entityPage %>/new']">
                <fa-icon icon="plus" />
                <span <% if (searchEngineAny) { %>class="hidden-sm-down" <% } %>>__jhiTranslateTag__('<%- i18nKeyPrefix %>.home.createLabel')</span>
            </button>
            <%_ } _%>
        </div>
    </h2>

    <<%= jhiPrefixDashed %>-alert-error />

    <<%= jhiPrefixDashed %>-alert />
<%_ if (searchEngineAny) { _%>

    <form name="searchForm" class="row row-cols-sm-auto align-items-center">
        <div class="col-sm-12">
            <div class="input-group w-100 mt-3">
                <label class="visually-hidden" for="currentSearch">__jhiTranslateTag__('<%- i18nKeyPrefix %>.home.search')</label>
                <input type="text" class="form-control" [(ngModel)]="currentSearch" id="currentSearch" name="currentSearch" placeholder="__jhiTranslatePipe__('<%= i18nKeyPrefix %>.home.search')">

                <button class="btn btn-info" (click)="search(currentSearch)">
                    <fa-icon icon="search" />
                </button>

            @if (currentSearch) {
                <button class="btn btn-danger" (click)="search('')">
                    <fa-icon icon="trash-alt" />
                </button>
            }
            </div>
        </div>
    </form>
<%_ } _%>

<%_ if (jpaMetamodelFiltering && paginationPagination) { _%>
    <<%= jhiPrefixDashed %>-filter [filters]="filters" />
<%_ } _%>

@if (<%= entityInstancePlural %>().length === 0) {
    <div class="alert alert-warning" id="no-result">
        <span>__jhiTranslateTag__('<%- i18nKeyPrefix %>.home.notFound')</span>
    </div>
} @else {
    <div class="table-responsive table-entities" id="entities">
        <table class="table table-striped" aria-describedby="page-heading">
            <thead>
                <tr <%= jhiPrefix %>Sort [(sortState)]="sortState" (sortChange)="navigateToWithComponentValues($event)">
<%_ for (const field of fields.filter(field => !field.hidden)) {
    const { fieldSupportsSortBy } = field;
_%>
                    <th scope="col"<% if (fieldSupportsSortBy) { %> <%= jhiPrefix %>SortBy="<%= field.fieldName %>"<% } %>>
                        <div class="d-flex">
                            <span>__jhiTranslateTag__('<%- field.fieldTranslationKey %>')</span>
  <%_ if (fieldSupportsSortBy) { _%>
                        <% if (searchEngineAny && !field.fieldTypeBoolean && !field.fieldTypeNumeric && !field.fieldTypeTemporal) { %>@if (!currentSearch) {<% } %>
                            <fa-icon class="p-1" icon="sort" />
                        <% if (searchEngineAny && !field.fieldTypeBoolean && !field.fieldTypeNumeric && !field.fieldTypeTemporal) { %>}<% } %>
  <%_ } _%>
                        </div>
                    </th>
<%_ } _%>
<%_ for (const relationship of relationships.filter(rel => !rel.otherEntity.embedded)) { _%>
  <%_ if (relationship.persistableRelationship && (!relationship.collection || paginationNo)) {
    const fieldName = "." + relationship.otherEntityField;
    const { fieldSupportsSortBy: relatedFieldSupportsSortBy } = relationship.relatedField;
  _%>
                    <th scope="col"<% if (relatedFieldSupportsSortBy) { %> <%= jhiPrefix %>SortBy="<%= relationship.relationshipName + (fieldName) %>"<% } %>>
                        <div class="d-flex">
                            <span>__jhiTranslateTag__('<%- i18nKeyPrefix %>.<%- relationship.relationshipName %>')</span>
    <%_ if (relatedFieldSupportsSortBy) { _%>
                            <fa-icon class="p-1" icon="sort" />
    <%_ } _%>
                        </div>
                    </th>
  <%_ } _%>
<%_ } _%>
                    <th scope="col"></th>
                </tr>
            </thead>
            <tbody<% if (paginationInfiniteScroll) { %> infinite-scroll (scrolled)="loadNextPage()" [infiniteScrollDisabled]="!hasMorePage()" [infiniteScrollDistance]="0"<% } %>>
            @for (<%= entityInstance %> of <%= entityInstancePlural %>(); track track<%= primaryKey.nameCapitalized %>(<%= entityInstance %>)) {
                <tr data-cy="entityTable">
<%_
const routerLink = ` [routerLink]="['/${ entityPage }', ${entityInstance}.${primaryKey.name}, 'view']"`;
for (field of fields.filter(field => !field.hidden)) {
    const fieldName = field.fieldName;
    const fieldType = field.fieldType;
_%>
    <%_ if (field.fieldTypeBinary && field.blobContentTypeImage) { _%>
                    <td>
                    @if (<%= entityInstance %>.<%= fieldName %>) {
                        <a (click)="openFile(<%= entityInstance %>.<%= fieldName %>, <%= entityInstance %>.<%= fieldName %>ContentType)">
                            <img [src]="'data:' + <%= entityInstance %>.<%= fieldName %>ContentType + ';base64,' + <%= entityInstance %>.<%= fieldName %>" style="max-height: 30px;" alt="<%= entityInstance %>"/>
                        </a>
                        <span>{{ <%= entityInstance %>.<%= fieldName %>ContentType }}, {{ byteSize(<%= entityInstance %>.<%= fieldName %>) }}</span>
                    }
                    </td>
    <%_ } else if (field.fieldTypeBinary && field.blobContentTypeAny) { _%>
                    <td>
                    @if (<%= entityInstance %>.<%= fieldName %>) {
                        <a (click)="openFile(<%= entityInstance %>.<%= fieldName %>, <%= entityInstance %>.<%= fieldName %>ContentType)">__jhiTranslateTag__('entity.action.open')</a>
                        <span>{{ <%= entityInstance %>.<%= fieldName %>ContentType }}, {{ byteSize(<%= entityInstance %>.<%= fieldName %>) }}</span>
                    }
                    </td>
    <%_ } else if (field.fieldIsEnum) { _%>
                    <%# TODO: import enum and use its key as label _%>
                    <td<% if (field.id) { %>><a<%- routerLink %><% } %>>__jhiTranslateTagEnum__('<%= frontendAppName %>.<%= fieldType %>', { "value": "<%= entityInstance %>.<%= fieldName %> ?? 'null'" })<% if (field.id) { %></a><% } %></td>
    <%_ } else if (field.fieldTypeTimed) { _%>
                    <td>{{ <%= entityInstance %>.<%= fieldName %> | formatMediumDatetime }}</td>
    <%_ } else if (field.fieldTypeLocalDate) { _%>
                    <td>{{ <%= entityInstance %>.<%= fieldName %> | formatMediumDate }}</td>
    <%_ } else if (field.fieldTypeDuration) { _%>
                    <td title="{{ <%= entityInstance %>.<%= fieldName %> }}">{{ <%= entityInstance %>.<%= fieldName %> | duration }}</td>
    <%_ } else { _%>
                    <td<% if (field.id) { %>><a<%- routerLink %><% } %>>{{ <%= entityInstance %>.<%= fieldName %> }}<% if (field.id) { %></a><% } %></td>
    <%_ } _%>
<%_ } _%>
<%_ for (const relationship of relationships.filter(rel => !rel.otherEntity.embedded)) {
    const relationshipFieldName = relationship.relationshipFieldName;
    const relationshipFieldNamePlural = relationship.relationshipFieldNamePlural;
    const otherEntityField = relationship.otherEntityField;
_%>
    <%_ if (relationship.persistableRelationship && (!relationship.collection || paginationNo)) { _%>
                    <td>
        <%_ if (relationship.otherEntityUser) { _%>
            <%_ if (relationship.collection) { _%>
                    @for (<%= relationshipFieldName %> of <%= entityInstance %>.<%= relationshipFieldNamePlural %>; track $index; let last = $last) {
                        <span>{{ <%= relationshipFieldName %>.<%= otherEntityField %> }}{{ last ? '' : ', ' }}</span>
                    }
            <%_ } else { _%>
                        {{ <%= entityInstance %>.<%= relationshipFieldName %>?.<%= otherEntityField %> }}
            <%_ } _%>
        <%_ } else { _%>
            <%_ if (relationship.collection) { _%>
                    @for (<%= relationshipFieldName %> of <%= entityInstance %>.<%= relationshipFieldNamePlural %>; track $index; let last = $last) {
                        <span>
                            <a class="form-control-static" [routerLink]="['/<%= relationship.otherEntity.entityPage %>', <%= relationshipFieldName %>.<%= relationship.otherEntity.primaryKey.name %>, 'view']">{{ <%= relationshipFieldName %>.<%= otherEntityField %> }}</a>{{ last ? '' : ', ' }}
                        </span>
                    }
            <%_ } else { _%>
                    @if (<%= entityInstance + "." + relationshipFieldName %>) {
                        <div>
                            <a [routerLink]="['/<%= relationship.otherEntity.entityPage %>', <%= entityInstance %>.<%= relationshipFieldName %>.<%= relationship.otherEntity.primaryKey.name %>, 'view']">{{ <%= entityInstance %>.<%= relationshipFieldName %>.<%= otherEntityField %> }}</a>
                        </div>
                    }
            <%_ } _%>
        <%_ } _%>
                    </td>
    <%_ } _%>
<%_ } _%>
                    <td class="text-end">
                        <div class="btn-group">
<%_ for (const relationship of relationships.filter(rel => !rel.otherEntity.embedded)) { _%>
    <%_ if (relationship.otherEntity.jpaMetamodelFiltering && relationship.otherEntity.paginationPagination && relationship.collection && !relationship.persistableRelationship) {
        const otherEntityTranslationKey = i18nKeyPrefix + '.' + relationship.relationshipName;
    _%>
                            <button type="submit"
                                    [routerLink]="['/<%= relationship.otherEntity.entityPage %>']"
                                    [queryParams]="{ 'filter[<%= relationship.otherEntityRelationshipName %>Id.in]': <%= entityInstance %>.<%= primaryKey.name %> }"
                                    class="btn btn-info btn-sm"
                                    data-cy="filterOtherEntityButton">
                                <fa-icon icon="eye" />
                                <span class="d-none d-md-inline">__jhiTranslateTagPipe__('entity.action.show', { "otherEntity": "<%= otherEntityTranslationKey %>" })</span>
                            </button>
    <%_ } _%>
<%_ } _%>
                            <a [routerLink]="['/<%= entityPage %>', <%= entityInstance %>.<%= primaryKey.name %>, 'view']"
                               class="btn btn-info btn-sm" data-cy="entityDetailsButton">
                                <fa-icon icon="eye" />
                                <span class="d-none d-md-inline">__jhiTranslateTag__('entity.action.view')</span>
                            </a>
<%_ if (!readOnly && updatableEntity) { _%>

                            <a [routerLink]="['/<%= entityPage %>', <%= entityInstance %>.<%= primaryKey.name %>, 'edit']"
                               class="btn btn-primary btn-sm" data-cy="entityEditButton">
                                <fa-icon icon="pencil-alt" />
                                <span class="d-none d-md-inline">__jhiTranslateTag__('entity.action.edit')</span>
                            </a>
<%_ } _%>
<%_ if (!readOnly) { _%>

                            <button type="submit" (click)="delete(<%= entityInstance %>)"
                                    class="btn btn-danger btn-sm"
                                    data-cy="entityDeleteButton">
                                <fa-icon icon="times" />
                                <span class="d-none d-md-inline">__jhiTranslateTag__('entity.action.delete')</span>
                            </button>
<%_ } _%>
                        </div>
                    </td>
                </tr>
            }
            </tbody>
        </table>
    </div>
}
<%_ if (paginationPagination) { _%>
@if (<%= entityInstancePlural %>().length > 0) {
    <div>
        <div class="d-flex justify-content-center">
            <<%= jhiPrefixDashed %>-item-count [params]="{ page, totalItems, itemsPerPage }" />
        </div>

        <div class="d-flex justify-content-center">
            <ngb-pagination [collectionSize]="totalItems" [page]="page" [pageSize]="itemsPerPage" [maxSize]="5" [rotate]="true" [boundaryLinks]="true" (pageChange)="navigateToPage($event)" />
        </div>
    </div>
}
<%_ } _%>
</div>
